﻿/**
* 模块名： viewModel
* 程序名: DayReport.js
**/
function viewModel() {
    var self = this;
    this.form = { Cxrq: ko.observable(), Info_Dwmc: ko.observable() };
    this.grid = {
        size: { w: 4, h: 78 },
        url: '/api/pacm/DayReport/' + ReportID,
        queryParams: ko.observable(),
        pagination: true,
        customLoad: false
    };
    //刷新
    this.refreshClick = function () {
        window.location.reload();
    };
    //添加一个日报表（弹出层）
    this.addClick = function () {
        com.dialog({
            title: "&nbsp;增加报表",
            iconCls: 'icon-node_tree',
            width: 310,
            height: 204,
            html: "#type-template",
            viewModel: function (w) {
                w.find("[id=txtName]").val(ReportName);
                w.find("[id=txtYear]").val("2014");
                this.confirmClick = function () {
                    //获取下列数据的值
                    var sYear = w.find("[id=txtYear]").val();
                    var sMonth = w.find("[id=txtMonth]").val();
                    var sDay = w.find("[id=txtDay]").val();
                    if (sYear == "" || sMonth == "" || sDay == "") {
                        com.message('warning', '信息不完整！');
                    } else {
                        $.ajax({
                            url: "/Pacm/DayReport/Add",
                            type: 'Post',
                            data: { reportID: ReportID, year: sYear, month: sMonth, day: sDay },
                            success: function (data) {
                                if (data == 1) {
                                    com.message('success', '添加成功！');
                                    w.dialog('close');
                                    self.searchClick();
                                } else if (data == 3) {
                                    com.message('warning', '该天的报表已存在！');
                                } else {
                                    com.message('warning', '添加失败！');
                                }
                            }
                        }).fail(function (xhr, textStatus, err) {
                            alert('Error: ' + err);
                        });
                    }
                }
                this.cancelClick = function () {
                    w.dialog('close');
                };
            }
        })
    };
    //编辑一个日报表（打开tab页）
    this.editClick = function () {
        self.showEditClick();
    };
    //打开对应的日报表
    this.showEditClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请选择之后进行操作！');
        com.openTab("日报编辑", "/pacm/DayReport/RirectEditReport/" + row["Info_Id"]);
    };
    //删除一条日报计量
    this.deleteClick = function () {

        var row = this.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请选择之后进行操作！');
        //先查询对应的表格是否有数据
        $.ajax({
            url: "/Pacm/DayReport/CheckDelete",
            type: 'GET',
            data: "id=" + row["Info_Id"],
            success: function (data) {
               // alert(data);
                if (data == "True") { //证明表格内已经有数据
                    com.message('confirm', '该表格已保存有数据，是否删除所有信息？', function (b) {
                        if (b) {
                            $.ajax({
                                url: "/Pacm/DayReport/Delete",
                                type: 'GET',
                                data: "id=" + row["Info_Id"] + "&all=true",
                                success: function (data) {
                                    com.message('success', '删除完成！');
                                    self.searchClick();
                                }
                            }).fail(function (xhr, textStatus, err) {
                                alert('Error: ' + err);
                            });
                        }
                    });
                } else {
                    com.message('confirm', '是否要删除该条记录吗？', function (b) {
                        if (b) {
                            $.ajax({
                                url: "/Pacm/DayReport/Delete",
                                type: 'GET',
                                data: "id=" + row["Info_Id"] + "&all=false",
                                success: function (data) {
                                    com.message('success', '删除完成！');
                                    self.searchClick();
                                }
                            }).fail(
                               function (xhr, textStatus, err) {
                                   alert('Error: ' + err);
                               });
                        }
                    });
                }
            }
        }).fail(
                function (xhr, textStatus, err) {
                    alert('Error: ' + err);
                });
    };
    //搜索
    this.searchClick = function () {
        var param = ko.toJS(this.form);
        this.grid.queryParams(param);
    }
    //清除条件
    this.clearClick = function () {
        $("#txtReportName").val("");
        $("#txtDate").val("");
    }
    //审核
    this.checkClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请选择之后进行操作！');
        $.ajax({
            url: "/Pacm/DayReport/Check",
            type: 'Post',
            data: { id: row["Info_Id"], type: 'check' },
            success: function (data) {
                com.message('success', '修改完成！');
                self.searchClick();
            }
        }).fail(
                function (xhr, textStatus, err) {
                    alert('Error: ' + err);
                });
    }
    //撤销
    this.cancelClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请选择之后进行操作！');
        $.ajax({
            url: "/Pacm/DayReport/Check",
            type: 'Post',
            data: { id: row["Info_Id"], type: 'cancel' },
            success: function (data) {
                com.message('success', '修改完成！');
                self.searchClick();
            }
        }).fail(
                function (xhr, textStatus, err) {
                    alert('Error: ' + err);
                });
    }
    //datagrid双击事件
    this.grid.onDblClickRow = this.showEditClick;
}
